static变量
视频锁定
{$ currentTime | date:'mm:ss' $}
{$ timeLeft | date:'mm:ss' $}
C语言中还有一种重要的变量类型,static
变量—静态变量,如果在一个局部变量前加上static
,那这个变量的存在期会延长,直到函数结束它还存在,如果没static
修饰的变量会随着函数结束而消失。
注意
static
修饰的局部变量虽然生存期很长,但是它始终是局部变量,不能在其他函数中使用。static
修饰全局变量没有意义。
现在我们用一个例子来说明static
变量的神奇:
#include <stdio.h>
void fun();
int main()
{
int i;
for(i=0;i<2;i++)
fun();
return 0;
}
void fun()
{
int b=1;
static int c=3;
b=b+1;
c=c+1;
printf("变量b的值为:%d,变量c的值为:%d\n",b,c);
}
在线练习
{$ activeFileHint $}